package com.wysiwyg.admin.service.mq;

import com.wysiwyg.admin.config.producer.FlowMessageProducer;
import com.wysiwyg.flow.constants.MessageConstants;
import com.wysiwyg.flow.enums.FlowEventEnum;
import com.wysiwyg.flow.enums.FlowTag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.apache.rocketmq.client.producer.SendCallback;

import java.util.Map;

/**
 * 流程消息服务
 * 封装消息业务逻辑，对外提供统一的接口
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class FlowMessageService {

    private final FlowMessageProducer flowMessageProducer;

    /**
     * 发送用户创建事件
     * @param userData 用户数据
     */
    public void sendUserCreatedEvent(Map<String, Object> userData) {
        String businessKey = MessageConstants.BUSINESS_KEY_USER_PREFIX + userData.get("userId");
        log.info("发送用户创建事件: businessKey={}, data={}", businessKey, userData);
        flowMessageProducer.sendAsync(FlowEventEnum.USER_CREATED, businessKey, userData);
    }

    /**
     * 发送用户更新事件
     * @param userData 用户数据
     */
    public void sendUserUpdatedEvent(Map<String, Object> userData) {
        String businessKey = MessageConstants.BUSINESS_KEY_USER_PREFIX + userData.get("userId");
        log.info("发送用户更新事件: businessKey={}, data={}", businessKey, userData);
        flowMessageProducer.sendAsync(FlowEventEnum.USER_UPDATED, businessKey, userData);
    }

    /**
     * 发送同步消息
     * @param eventType 事件类型
     * @param businessKey 业务键
     * @param payload 消息载荷
     */
    public <T> void sendSyncMessage(FlowEventEnum eventType, String businessKey, T payload) {
        log.info("发送同步消息: eventType={}, businessKey={}", eventType, businessKey);
        flowMessageProducer.sendSync(eventType, businessKey, payload);
    }

    /**
     * 发送异步消息
     * @param eventType 事件类型
     * @param tag 业务流程标签
     * @param businessKey 业务键
     * @param payload 消息载荷
     */
    public <T> void sendAsyncMessage(FlowEventEnum eventType, FlowTag tag, String businessKey, T payload) {
        sendAsyncMessage(eventType, tag, businessKey, payload, null, null);
    }

    /**
     * 发送异步消息，支持自定义回调处理
     * @param eventType 事件类型
     * @param tag 业务流程标签
     * @param businessKey 业务键
     * @param payload 消息载荷
     * @param onSuccess 成功回调（可选）
     * @param onFailure 失败回调（可选）
     */
    public <T> void sendAsyncMessage(FlowEventEnum eventType, FlowTag tag, String businessKey, T payload,
                                    SendCallback onSuccess, SendCallback onFailure) {
        log.info("发送异步消息: eventType={}, tag={}, businessKey={}", eventType, tag, businessKey);
        flowMessageProducer.sendAsync(eventType, tag, businessKey, payload, onSuccess, onFailure);
    }

    // 保留旧的方法签名以向后兼容
    /**
     * @deprecated 使用带tag参数的方法
     */
    @Deprecated
    public <T> void sendAsyncMessage(FlowEventEnum eventType, String businessKey, T payload) {
        sendAsyncMessage(eventType, null, businessKey, payload, null, null);
    }

    /**
     * @deprecated 使用带tag参数的方法
     */
    @Deprecated
    public <T> void sendAsyncMessage(FlowEventEnum eventType, String businessKey, T payload,
                                    SendCallback onSuccess, SendCallback onFailure) {
        sendAsyncMessage(eventType, null, businessKey, payload, onSuccess, onFailure);
    }
}
